﻿using System;
using System.Text;

namespace FuzzScript
{
    internal class FuzzyRule : Node
    {
        public override double Execute()
        {
            double dDoM = AntecedentExpression.Execute();

            if (dDoM > 0)
            {
                for (int i = 1; i < m_SiblingNodes.Count; i++)
                {
                    ((IConsequentClause)m_SiblingNodes[i]).InferFuzzyOutput(dDoM);
                }
            }
            return 0;
        }


        public override string Generate()
        {
            StringBuilder sCode = new StringBuilder();
            sCode.Append("dAlpha = "+AntecedentExpression.Generate()+"   ;\n");

            sCode.Append("if(dAlpha > 0 )\n{\n");

            for (int i = 1; i < m_SiblingNodes.Count; i++)
            {
                sCode.Append((m_SiblingNodes[i]).Generate());
                sCode.Append(", dAlpha );\n");
            }

            sCode.Append("}\n");

            return sCode.ToString();
        }

        public FuzzyRule()
        {
            m_SiblingNodes.Add(null);
        }

        public INode AntecedentExpression
        {
            get { return m_SiblingNodes[0]; }
            set { m_SiblingNodes[0] = value; }
        }


        public override void AddSibling(INode p_SiblingNode)
        {
            base.AddSibling(p_SiblingNode);
        }
    }
}
